// pages/my/fuwu/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: 0, shequ_id: 0,
    glist: [], //商品数据
    page: 1, size: 4, //分页
    ispop: 0, id: 0, code: '',
    title: '', cover: '', price: '', price_old: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.userInfo();
    this.goodsList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 登录信息
  userInfo: function (e) {
    var uinfo = wx.getStorageSync('uinfo');
    if (!uinfo) {
      wx.showToast({
        title: '没有登录', icon: 'none',
      }); return
    }
    this.setData({ 
      uid: uinfo.uid, shequ_id: uinfo.shequ_id,
    });
  },

  // 获取数据
  goodsList: function (e) {
    var uinfo = wx.getStorageSync('uinfo');
    console.log(uinfo);
    var _this = this;
    var formData = {
      page: _this.data.page,
      size: _this.data.size,
      uid: uinfo.uid,
    };
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'yw-mall/fuwu/list',
      data: formData,
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        var d = res.data;
        var glist = _this.data.glist;
        for (var i = 0; i < d.length; i ++) {
          glist.push(d[i]);
        }
        _this.setData({
          glist: d,
        });
      },
      error: function (err) {
        console.log(err);
      }
    });
  },

  // 去添加
  popAdd: function (e) {
    this.setData({
      ispop: 1, 
      id: 0, code: '',
      title: '', cover: '', 
      price: '', price_old: '',
    });
  },

  // 去修改
  popEdit: function (e) {
    console.log(e); //return
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    var code = e.currentTarget.dataset.code;
    var cover = e.currentTarget.dataset.cover;
    var price = e.currentTarget.dataset.price;
    var price_old = e.currentTarget.dataset.price_old;
    this.setData({
      ispop: 1,
      id: id, code: code,
      title: title, cover: cover, 
      price: price, price_old: price_old,
    });
  },

  // 选择图片
  chooseImage() {
    var _this = this;
    wx.chooseMedia({
      count: 1, // 最多选择1张
      mediaType: ['image'], // 只允许选择图片
      sourceType: ['album', 'camera'], // 相册和相机
      success: (res) => {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        console.log(tempFilePath);
        // this.setData({
        //   tempFilePath: tempFilePath // 临时文件路径，用于预览
        // });
        wx.showLoading({
          title: '上传中...',
        });
        wx.uploadFile({
          url: app.globalData.xcxApi + 'scan/img/upload', 
          filePath: tempFilePath,
          name: 'img', 
          formData: {
            uid: this.data.uid,
          },
          success(res) {
            console.log(res);
            wx.hideLoading();
            res = JSON.parse(res.data); // 后端返回的数据
            if (res.code === 0) {
              wx.showToast({
                title: '上传成功',
                icon: 'success'
              });
              _this.setData({
                cover: res.data.link, 
              });
            } else {
              wx.showToast({
                title: '上传失败', icon: 'none'
              });
            }
          },
          fail(err) {
            wx.hideLoading();
            wx.showToast({
              title: '上传失败err', icon: 'none'
            });
          }
        });
      }
    });
  },

  // 服务名称
  toTitle: function (e) {
    console.log(e);
    this.setData({
      title: e.detail.value,
    });
  },

  // 单价
  toPrice: function (e) {
    console.log(e);
    this.setData({
      price: e.detail.value,
    });
  },

  // 原价
  toPriceold: function (e) {
    console.log(e);
    this.setData({
      price_old: e.detail.value,
    });
  },

  // 弹窗关闭
  popClose: function (e) {
    this.setData({
      ispop: 0, id: 0,
      code: '', title: '', cover: '',
      price: 0, price_old: 0, 
    });
  },
  
  // 保存
  toSave: function (e) {
    var _this = this;
    var formData = {
      id: _this.data.id,
      uid: _this.data.uid,
      shequ_id: _this.data.shequ_id,
      code: _this.data.code, 
      title: _this.data.title, 
      cover: _this.data.cover,
      price: _this.data.price, 
      price_old: _this.data.price_old, 
    };
    console.log(formData); //return
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'yw-mall/fuwu/modify',
      data: formData,
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.setData({ 
          ispop: 0, id: 0,
          code: '', title: '', cover: '',
          price: 0, price_old: 0, 
        });
        _this.goodsList();
      },
      error: function (err) {
        console.log(err);
      }
    });
  },
})